/*
 * test1
 */
package com.pitop.errortype.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.ErrortypeLocalException;
import com.pitop.errortype.dao.ErrortypeLocalDAO;
import com.pitop.errortype.entity.ErrortypeLocalState;
import com.pitop.errortype.entity.ErrortypeLocal;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("errortypeLocalSV")
@Slf4j
public class ErrortypeLocalSVImpl extends BaseSVImpl<ErrortypeLocal, Long> implements ErrortypeLocalSV {

    @Autowired
    private ErrortypeLocalDAO errortypeLocalDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return errortypeLocalDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(ErrortypeLocal entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象ErrortypeLocal
     *
     * @param id * @param errorTypeCode 错误类型编码* @param language 语言
     * @return ErrortypeLocal
     */
    @Override
    public ErrortypeLocal load(Long id, String errorTypeCode, String language) {
        if (id == null && errorTypeCode == null && language == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("errorTypeCode", errorTypeCode);
        param.put("language", language);
        return errortypeLocalDAO.load(param);
    }

    /**
     * 加载一个对象ErrortypeLocal 通过id
     *
     * @param id
     * @return ErrortypeLocal
     */
    @Override
    public ErrortypeLocal loadById(Long id) {
        if (id == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return errortypeLocalDAO.loadById(id);
    }

    /**
     * 加载一个对象ErrortypeLocal 通过errorTypeCode
     *
     * @param errorTypeCode 错误类型编码
     * @return ErrortypeLocal
     */
    @Override
    public ErrortypeLocal loadByErrorTypeCode(String errorTypeCode) {
        if (errorTypeCode == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return errortypeLocalDAO.loadByErrorTypeCode(errorTypeCode);
    }

    /**
     * 加载一个对象ErrortypeLocal 通过language
     *
     * @param language 语言
     * @return ErrortypeLocal
     */
    @Override
    public ErrortypeLocal loadByLanguage(String language) {
        if (language == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return errortypeLocalDAO.loadByLanguage(language);
    }


    /**
     * 根据主键id,oldStates 共同更新 ErrortypeLocal 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, ErrortypeLocalState newState, ErrortypeLocalState... oldStates) {
        if (id == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        errortypeLocalDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键errorTypeCode,oldStates 共同更新 ErrortypeLocal 的状态到newState状态
     *
     * @param errorTypeCode 错误类型编码
     * @param newState      新状态
     * @param oldStates     旧状态集合
     */
    @Override
    public void updateStateByErrorTypeCode(String errorTypeCode, ErrortypeLocalState newState, ErrortypeLocalState... oldStates) {
        if (errorTypeCode == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        errortypeLocalDAO.updateStateByErrorTypeCode(errorTypeCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键language,oldStates 共同更新 ErrortypeLocal 的状态到newState状态
     *
     * @param language  语言
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByLanguage(String language, ErrortypeLocalState newState, ErrortypeLocalState... oldStates) {
        if (language == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        errortypeLocalDAO.updateStateByLanguage(language, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 ErrortypeLocal 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, ErrortypeLocalState state) {
        if (id == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        errortypeLocalDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键errorTypeCode 更新 ErrortypeLocal 的状态到另一个状态
     *
     * @param errorTypeCode 错误类型编码
     * @param state         状态
     */
    @Override
    public void updateByErrorTypeCode(String errorTypeCode, ErrortypeLocalState state) {
        if (errorTypeCode == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        errortypeLocalDAO.updateByErrorTypeCode(errorTypeCode, state.name(), new Date());
    }

    /**
     * 根据主键language 更新 ErrortypeLocal 的状态到另一个状态
     *
     * @param language 语言
     * @param state    状态
     */
    @Override
    public void updateByLanguage(String language, ErrortypeLocalState state) {
        if (language == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        errortypeLocalDAO.updateByLanguage(language, state.name(), new Date());
    }


    /**
     * 删除对象ErrortypeLocal
     *
     * @param id * @param errorTypeCode 错误类型编码* @param language 语言
     * @return ErrortypeLocal
     */
    @Override
    public void delete(Long id, String errorTypeCode, String language) {
        if (id == null && errorTypeCode == null && language == null) {
            throw new ErrortypeLocalException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("errorTypeCode", errorTypeCode);
        param.put("language", language);
        errortypeLocalDAO.delete(param);
    }


    /**
     * 查询ErrortypeLocal分页
     *
     * @param errortypeLocal 对象
     * @param offset         查询开始行
     * @param limit          查询行数
     * @return List<ErrortypeLocal>
     */
    @Override
    public List<ErrortypeLocal> list(ErrortypeLocal errortypeLocal, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (errortypeLocal != null) {
            map = JSON.parseObject(JSON.toJSONString(errortypeLocal, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return errortypeLocalDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(ErrortypeLocal errortypeLocal) {
        Map<String, Object> map = null;
        if (errortypeLocal != null) {
            map = JSON.parseObject(JSON.toJSONString(errortypeLocal, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return errortypeLocalDAO.count(map);
    }

    /**
     * 查询ErrortypeLocal分页
     *
     * @param id     * @param errorTypeCode  错误类型编码* @param language  语言
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<ErrortypeLocal>
     */
    @Override
    public List<ErrortypeLocal> list(Long id, String errorTypeCode, String language, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("errorTypeCode", errorTypeCode);
        param.put("language", language);
        return errortypeLocalDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String errorTypeCode, String language) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("errorTypeCode", errorTypeCode);
        param.put("language", language);
        return errortypeLocalDAO.count(param);
    }

}
